package com.heart.house.biz.service;


import com.google.common.collect.Lists;
import com.heart.house.biz.mapper.UserMapper;
import com.heart.house.common.model.User;
import com.heart.house.common.utils.BeanHelper;
import com.heart.house.common.utils.HashUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Service
public class UserService {


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FileService fileService;
    @Autowired
    private MailService mailService;

    @Value("${file.prefix}")
    private String imgPrefix;


    public List<User> getUsers() {
        return userMapper.selectUsers();
    }

    /**
     * 1.插入数据库 非激活 密码加盐MD5 保存头像
     * 2.生成key 绑定email
     * 3.发送邮件给用户
     *
     * @param account
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addAccount(User account) {
        account.setPasswd(HashUtils.encryPassword(account.getPasswd()));
        List<String> imgList = fileService.getImgPath(Lists.newArrayList(account.getAvatarFile()));
        if (!imgList.isEmpty()) {
            account.setAvatar(imgList.get(0));
        }
        BeanHelper.setDefaultProp(account, User.class);
        BeanHelper.onInsert(account);
        account.setEnable(0);

        userMapper.insert(account);
        mailService.registerNotify(account.getEmail());
        return true;
    }


    public boolean enable(String key) {
        return mailService.enable(key);
    }

    /**
     * 用户名 密码验证
     *
     * @param username
     * @param passwd
     * @return
     */
    public User auth(String username, String passwd) {
        User user = new User();
        user.setEmail(username);
        user.setPasswd(HashUtils.encryPassword(passwd));
        user.setEnable(1);

        List<User> list = getUserByQuery(user);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public List<User> getUserByQuery(User user) {
        List<User> userList = userMapper.selectUsersByQuery(user);
        userList.forEach(u -> {
            u.setAvatar(imgPrefix + u.getAvatar());
        });
        return userList;
    }

    public void updateUser(User updateUser, String email) {
        updateUser.setEmail(email);
        BeanHelper.onUpdate(updateUser);

        userMapper.update(updateUser);
    }

    public boolean findByEmail(String email) {
        int count = userMapper.findByEmail(email);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改用户头像
     * 1.根据id 查询头像图片并删除
     * 2.上传新图片
     * 3.更新用户信息
     *
     * @param userId
     * @param avatarFile
     */
    public List<User> uploadFile(Long userId, MultipartFile avatarFile) {
        // todo 删除原有的图片即文件夹
        User updateUser = new User();
        updateUser.setId(userId);
        List<String> imgList = fileService.getImgPath(Lists.newArrayList(avatarFile));
        if (!imgList.isEmpty()) {
            updateUser.setAvatar(imgList.get(0));
        }
        // 更新数据库
        userMapper.updateById(updateUser);
        return getUserByQuery(updateUser);
    }
}
